<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter"
      :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="模板id" prop="templateId">
              <el-input v-model="queryParams.templateId" placeholder="请输入模板id" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="模板对象" prop="template">
              <el-input v-model="queryParams.template" placeholder="请输入模板对象" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="用户信息" prop="userInfo">
              <el-input v-model="queryParams.userInfo" placeholder="请输入用户信息" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="商业地址" prop="businessAddress">
              <el-input v-model="queryParams.businessAddress" placeholder="请输入商业地址" clearable
                @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="维度" prop="latitude">
              <el-input v-model="queryParams.latitude" placeholder="请输入维度" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="经度" prop="longitude">
              <el-input v-model="queryParams.longitude" placeholder="请输入经度" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="旧套餐" prop="oldPlans">
              <el-input v-model="queryParams.oldPlans" placeholder="请输入旧套餐" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="新套餐" prop="newPlans">
              <el-input v-model="queryParams.newPlans" placeholder="请输入新套餐" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd"
              v-hasPermi="['business:businessInfo:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()"
              v-hasPermi="['business:businessInfo:edit']">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()"
              v-hasPermi="['business:businessInfo:remove']">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport"
              v-hasPermi="['business:businessInfo:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="businessInfoList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="id" align="center" prop="id" v-if="true" />
        <!-- <el-table-column label="模板id" align="center" prop="templateId" /> -->
        <!-- <el-table-column label="模板对象" align="center" prop="template" /> -->
        <el-table-column label="填报人信息" align="center">
          <template #default="scope">
            <div v-for="(item, index) in scope.row.template.userInfo">
              {{ item }} : {{ scope.row.userInfo[index] }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="商机地址" align="center" prop="businessAddress" show-overflow-tooltip />
        <el-table-column label="经维度" align="center">
          <template #default="scope">
            {{ scope.row.latitude }},{{ scope.row.longitude }}
          </template>
        </el-table-column>
        <el-table-column label="配置信息" show-overflow-tooltip>
          <template #default="scope">
            <el-collapse v-model="scope.row.collapseVisible">
              <el-collapse-item title="套餐详情">
                <div v-for="(item, index) in scope.row.oldPlans" :key="index">
                  <el-collapse-item :title="'原有套餐' + (index + 1) + ':'">
                    <div v-for="(item3, index3) in scope.row.template.plan" :key="index3" style="padding-left: 0; margin-left: 0;" >
                      <span>{{ item3 }}:{{ item[index3] }}</span>
                    </div>
                    <el-collapse>
                      <div v-for="(item2, index2) in scope.row.newPlans[index]" :key="index2">
                        <el-collapse-item :title="'升级方案' + (index2 + 1) + ':'">
                          <div v-for="(item4, index4) in scope.row.template.plan" :key="index4" style="position: relative; left: 20px;">
                            <span>{{ item4 }}:{{ item2[index4] }}</span>
                          </div>
                        </el-collapse-item>
                      </div>
                    </el-collapse>
                  </el-collapse-item>
                </div>
              </el-collapse-item>
            </el-collapse>
          </template>
        </el-table-column>
        <el-table-column label="填报时间" align="center" prop="createTime" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                v-hasPermi="['business:businessInfo:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                v-hasPermi="['business:businessInfo:remove']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
    <!-- 添加或修改比算表信息对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
      <el-form ref="businessInfoFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="模板id" prop="templateId">
          <el-input v-model="form.templateId" placeholder="请输入模板id" />
        </el-form-item>
        <el-form-item label="模板对象" prop="template">
          <el-input v-model="form.template" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="用户信息" prop="userInfo">
          <el-input v-model="form.userInfo" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="商业地址" prop="businessAddress">
          <el-input v-model="form.businessAddress" placeholder="请输入商业地址" />
        </el-form-item>
        <el-form-item label="维度" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入维度" />
        </el-form-item>
        <el-form-item label="经度" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="旧套餐" prop="oldPlans">
          <el-input v-model="form.oldPlans" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="新套餐" prop="newPlans">
          <el-input v-model="form.newPlans" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="BusinessInfo" lang="ts">
import { listBusinessInfo, getBusinessInfo, delBusinessInfo, addBusinessInfo, updateBusinessInfo } from '@/api/business/businessInfo';
import { BusinessInfoVO, BusinessInfoQuery, BusinessInfoForm } from '@/api/business/businessInfo/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const businessInfoList = ref<BusinessInfoVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const businessInfoFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: BusinessInfoForm = {
  id: undefined,
  templateId: undefined,
  template: undefined,
  userInfo: undefined,
  businessAddress: undefined,
  latitude: undefined,
  longitude: undefined,
  oldPlans: undefined,
  newPlans: undefined,
}
const data = reactive<PageData<BusinessInfoForm, BusinessInfoQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    templateId: undefined,
    template: undefined,
    userInfo: undefined,
    businessAddress: undefined,
    latitude: undefined,
    longitude: undefined,
    oldPlans: undefined,
    newPlans: undefined,
    params: {
    }
  },
  rules: {
    id: [
      { required: true, message: "id不能为空", trigger: "blur" }
    ],
    templateId: [
      { required: true, message: "模板id不能为空", trigger: "blur" }
    ],
    template: [
      { required: true, message: "模板对象不能为空", trigger: "blur" }
    ],
    userInfo: [
      { required: true, message: "用户信息不能为空", trigger: "blur" }
    ],
    businessAddress: [
      { required: true, message: "商业地址不能为空", trigger: "blur" }
    ],
    latitude: [
      { required: true, message: "维度不能为空", trigger: "blur" }
    ],
    longitude: [
      { required: true, message: "经度不能为空", trigger: "blur" }
    ],
    oldPlans: [
      { required: true, message: "旧套餐不能为空", trigger: "blur" }
    ],
    newPlans: [
      { required: true, message: "新套餐不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询比算表信息列表 */
const getList = async () => {
  loading.value = true;
  const res = await listBusinessInfo(queryParams.value);
  businessInfoList.value = res.rows;
  console.log("查询返回的数据：", res)
  total.value = res.total;
  loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  businessInfoFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: BusinessInfoVO[]) => {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = "添加比算表信息";
}

/** 修改按钮操作 */
const handleUpdate = async (row?: BusinessInfoVO) => {
  reset();
  const _id = row?.id || ids.value[0]
  const res = await getBusinessInfo(_id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = "修改比算表信息";
}

/** 提交按钮 */
const submitForm = () => {
  businessInfoFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateBusinessInfo(form.value).finally(() => buttonLoading.value = false);
      } else {
        await addBusinessInfo(form.value).finally(() => buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess("操作成功");
      dialog.visible = false;
      await getList();
    }
  });
}

/** 删除按钮操作 */
const handleDelete = async (row?: BusinessInfoVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除比算表信息编号为"' + _ids + '"的数据项？').finally(() => loading.value = false);
  await delBusinessInfo(_ids);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('business/businessInfo/export', {
    ...queryParams.value
  }, `businessInfo_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  getList();
});
</script>
